package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfSignNo;
import com.ocom.common.entity.typehandler.SignNoHandle;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfSignNo;
import com.ocom.payconfig.service.XfSignNoService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfSignNoServiceImpl extends ServiceImpl<IDaoXfSignNo, EntityXfSignNo> implements XfSignNoService {


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfSignNo> getSignList(Long companyId, Long pId) {
        QueryWrapper<EntityXfSignNo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("p_id", pId);
        queryWrapper.eq("status", 1);
        return this.list(queryWrapper);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean addSignNo(Long companyId, EntityXfSignNo entityXfSignNo) {
        SignNoHandle signNoHandle = new SignNoHandle();
        entityXfSignNo.setSignNo(signNoHandle.encrypt(entityXfSignNo.getSignNo(), 1l));
        return this.save(entityXfSignNo);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean delSignNo(Long companyId, EntityXfSignNo entityXfSignNo) {
        UpdateWrapper<EntityXfSignNo> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", entityXfSignNo.getComId());
        updateWrapper.eq("merch_id", entityXfSignNo.getMerchId());
        updateWrapper.eq("p_id", entityXfSignNo.getPId());
        updateWrapper.eq("pay_way", entityXfSignNo.getPayWay());
        updateWrapper.eq("status", 1);
        updateWrapper.set("status", 0);
        if (!CommonUtil.isNull(entityXfSignNo.getRemark())) {
            if (entityXfSignNo.getRemark().length() >= 255) {
                entityXfSignNo.setRemark(entityXfSignNo.getRemark().substring(0, 250));
            }
            updateWrapper.set("remark", entityXfSignNo.getRemark());
        }
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfSignNo getUserPayWay(Long companyId, Long pId, Integer merchId, Integer payWay) {
        QueryWrapper<EntityXfSignNo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", merchId);
        queryWrapper.eq("p_id", pId);
        queryWrapper.eq("pay_way", payWay);
        queryWrapper.eq("status", 1);
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfSignNo getUserSignInfo(Long companyId, Long pId, Integer merchId, Integer payWay, Integer status) {
        QueryWrapper<EntityXfSignNo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("p_id", pId);
        if(merchId != null && merchId != -1){
            queryWrapper.eq("merch_id", merchId);
        }
        queryWrapper.eq("pay_way", payWay);
        if (null != status && -1 != status) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
